package com.tsystems.tuni.sportplanet.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Implementation of GenericDAO.
 * 
 * @author Yuriy Eremin
 * 
 * @param <T>
 */
public class GenericDAOImpl<T> implements GenericDAO<T> {

	public static final EntityManagerFactory emf = Persistence
			.createEntityManagerFactory("sportplanetPU");
	public static EntityManager em = emf.createEntityManager();

	/**
	 * Save Entity in Database.
	 */
	public void save(T entity) {
		/*
		 * Session hibernateSession = this.getSession(); Transaction tr =
		 * hibernateSession.beginTransaction(); try {
		 * hibernateSession.save(entity); tr.commit();
		 * System.out.println("entity saved!"); } catch (Exception ex) {
		 * tr.rollback(); System.out.println("some error in commit!"); } finally
		 * { hibernateSession.close(); }
		 */
	}

	public void merge(T entity) {
		/*
		 * Session hibernateSession = this.getSession();
		 * hibernateSession.merge(entity);
		 */
	}

	public void delete(T entity) {
		/*
		 * Session hibernateSession = this.getSession();
		 * hibernateSession.delete(entity);
		 */
	}

	/*
	 * @SuppressWarnings("unchecked") public List<T> findMany(Query query) {
	 * List<T> t; t = (List<T>) query.list(); return t; }
	 * 
	 * @SuppressWarnings("unchecked") public T findOne(Query query) { T t; t =
	 * (T) query.uniqueResult(); return t; }
	 */

	@SuppressWarnings("unchecked")
	public List<T> getAll(Class<T> clazz) {
		Query query = em.createQuery("from " + clazz.getName());
		return query.getResultList();
	}

	@SuppressWarnings("rawtypes")
	public T findById(Class clazz, Integer id) {
		T t = null;
		/*
		 * Session hibernateSession = this.getSession(); T t = null; t = (T)
		 * hibernateSession.get(clazz, id);
		 */
		return t;
		// TODO need to refactoring.
	}

}
